/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.core.plugin;

import java.util.HashMap;
import java.util.Map;

import com.ewerp.mud.commands.EwerpCommandEngine;
import com.ewerp.mud.core.EwerpLog;
import com.ewerp.mud.core.EwerpMud;
import com.ewerp.mud.core.EwerpProperties;
import com.ewerp.mud.core.EwerpSessionManager;
import com.ewerp.mud.core.exceptions.EwerpMudException;
import com.ewerp.mud.lifecycle.ILifecycleListener;
import com.ewerp.mud.lifecycle.ILifecycleProvider;
import com.ewerp.mud.locations.rooms.EwerpRoomManager;
import com.ewerp.mud.plugin.IPluginManager;
import com.ewerp.mud.storage.EwerpPersistenceManager;

public class EwerpPluginManager implements IPluginManager {
    protected static EwerpPluginManager instance = new EwerpPluginManager();
    
    protected ILifecycleProvider lifecycleProvider = null;
    
    protected static Map<String,Map<Class<?>, IPlugin>> plugins = new HashMap<String,Map<Class<?>, IPlugin>>();
    
    public static EwerpPluginManager getInstance() {
        if(instance == null) {
            instance = new EwerpPluginManager();
        }
        return instance;
    }
    
    protected EwerpPluginManager() {
    }
    
    @Override
	public IPlugin getPlugin(String namespace, Class<?> clazz) throws EwerpMudException {
    	validateNamespace(namespace);
    	
    	Map<Class<?>, IPlugin> pluginMap = plugins.get(namespace);
    	
    	if(pluginMap != null) {
    		return pluginMap.get(clazz);
    	}
		
		return null;
	}
    
    @Override
    public void addPlugin(IPlugin plugin) throws EwerpMudException {
    	addPlugin(EwerpMud.defaultNamespace, plugin);
    }
    
    @Override
	public void addPlugin(String namespace, IPlugin plugin) throws EwerpMudException {
    	Map<Class<?>, IPlugin> pluginMap = null;
    	
		for(Class<?> clazz : plugin.getInterfaces()) {
			pluginMap = plugins.get(namespace);
			if(pluginMap == null) {
				plugins.put(namespace, pluginMap = new HashMap<Class<?>, IPlugin>());
			}
			
			pluginMap.put(clazz, plugin);
		}
	}

    protected void validateNamespace(String namespace) throws EwerpMudException{
        if(!namespace.equals(EwerpMud.defaultNamespace)) {
            throw new EwerpMudException("Namespace '" + namespace + "' could not be found");
        }
    }
    
    protected void registerAsLifecycleListener(Object o) {
        if(o instanceof ILifecycleListener) {
            lifecycleProvider.registerListener((ILifecycleListener)o);
        } else {
            System.err.println("Failed to register lifecycle listener: " + o.getClass());
        }
    }

    @Override
    public void setLifecycleProvider(ILifecycleProvider lifecycleProvider) throws EwerpMudException {
        this.lifecycleProvider = lifecycleProvider;
    }

    @Override
    public void init() {
        try {
        	addPlugin(EwerpLog.getInstance());
        	addPlugin(EwerpProperties.getInstance());
        	addPlugin(EwerpSessionManager.getInstance());
        	addPlugin(EwerpRoomManager.getInstance());
            addPlugin(EwerpPersistenceManager.getInstance());
            addPlugin(EwerpCommandEngine.getInstance());
            EwerpCommandEngine.getInstance().setPluginManager(this);

            // Register all applicable plugins as LifecycleListeners
            for(Map<Class<?>, IPlugin> pluginMap : plugins.values()) {
            	for(IPlugin plugin : pluginMap.values()) {
            		if(plugin instanceof ILifecycleListener) {
            			registerAsLifecycleListener(plugin);
            		}
            	}
            }

        } catch(Exception e) {
        	e.printStackTrace();
        }
    }

	
}
